package nl.gamayun.whohasit;

import java.util.List;

import nl.gamayun.whohasit.utils.DurationFormat;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LoanAdapter extends ArrayAdapter<Loan>
{
	static final private String	TAG = LoanAdapter.class.getSimpleName();
	int	resource;
	//private Resources resources= WhoHasIt.getContext().getResources();
	
	public LoanAdapter(Context _context, int _resource, List<Loan> _items)
	{
		super(_context, _resource, _items);
		resource = _resource;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		Log.d(TAG, "getView");
		LinearLayout LoanListItemView;
		Loan loan = getItem(position);
		if (convertView == null)
		{
			LoanListItemView = new LinearLayout(getContext());
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater vi;
			vi = (LayoutInflater) getContext().getSystemService(inflater);
			vi.inflate(resource, LoanListItemView, true);
		}
		else
		{
			LoanListItemView = (LinearLayout) convertView;
		}
		
		TextView header = (TextView) LoanListItemView.findViewById(R.id.loanlist_item_header);
		header.setText(loan.getHeader());
		TextView loanerName = (TextView) LoanListItemView.findViewById(R.id.loanlist_item_loanerName);
		loanerName.setText(loan.getLoanerName());
		TextView type = (TextView) LoanListItemView.findViewById(R.id.loanlist_item_type);
		type.setText(loan.getType().getTypeValue());
		TextView date = (TextView) LoanListItemView.findViewById(R.id.loanlist_item_date);
		date.setText(DurationFormat.getDurationTillTodayBig(loan.getDate()));
		TextView returnDate = (TextView) LoanListItemView.findViewById(R.id.loanlist_item_return_date);
		returnDate.setText(DurationFormat.getDurationFromTodayBig(loan.getReturnDate()));
		if (loan.isToLate())
		{
			returnDate.setTextAppearance(getContext(), R.style.LoanlistItemDateToLate);
		}
		else
		{
			returnDate.setTextAppearance(getContext(), R.style.LoanlistItemDate);
		}
/*		ImageButton buttonAdd = (ImageButton) LoanListItemView.findViewById(R.id.button_delete);
		buttonAdd.setOnClickListener(new OnClickListener()
		{
			public void onClick(View _view)
			{
				Log.d(TAG, "onClick Delete Button");
				DeleteDialog deleteDialog = new DeleteDialog(_view.getContext(), R.style.Dialog);
				deleteDialog.show();
			}
		});
*/
		return LoanListItemView;
	}

}
